package com.sunnada.edu.system.sysarea.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunnada.edu.system.pub.common.Common;
import com.sunnada.edu.system.sysarea.dao.AreaDao;

@Service
@Transactional
public class AreaServiceImpl implements AreaService {
	@Resource
	AreaDao areaDao;

	@Override
	public List<?> areaTree(HashMap<String, String> params) {
		return areaDao.areaTree(params);
	}

	@Override
	public int getAreaCots(HashMap<String, String> params) {
		return areaDao.getAreaCots(params);
	}

	@Override
	public List<?> findAreas(HashMap<String, String> params) {
		return areaDao.findAreas(params);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String areaAdd(HashMap<String, String> params) {
		String areaLName = params.get("areaSName"); // 简称
		Map area = areaDao.getAreaByAreacord(params);
		String areaParenLName = area.get("AREA_LNAME").toString();
		areaLName = areaParenLName + "-" + areaLName;
		params.put("areaLName", areaLName); // 全称
		String msg = "";
		String bigCode = areaDao.areaTreebig(params); // 获取父区域编码下的最大子区域编码
		if ("0".equals(bigCode)) { // 等于0表示该父区域下没有子区域
			bigCode = params.get("areaCode") + "10";
		} else {
			bigCode = String.valueOf(Integer.valueOf(bigCode).intValue() + 1);
		}
		params.put("parentCode", bigCode); // 内部区域编码
		params.put("recId", UUID.randomUUID().toString());
		int num = areaDao.areaAdd(params); // 往区域表添加
		if (num == 0) {
			msg = "添加失败！";
		}
		return msg;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map getAreaByRecId(HashMap<String, String> params) {
		return areaDao.getAreaByRecId(params);
	}

	@Override
	public String areaUpdate(HashMap<String, String> params) {
		String areaLName = params.get("areaSName");
		String areaParenLName = (String) (areaDao.getAreaByAreacord(params).get("AREA_LNAME"));
		areaLName = areaParenLName + "-" + areaLName;
		params.put("areaLName", areaLName);
		String msg = "";
		int num = areaDao.areaUpdate(params);
		if (num == 0) {
			msg = "修改失败！";
		}
		return msg;
	}

	@Override
	public String areaDel(HashMap<String, String> params) {
		String msg = "";
		int num = areaDao.areaDel(params);
		if (num == 0) {
			msg = "删除失败！";
		}
		return msg;
	}

	@Override
	public String areaEnable(HashMap<String, String> params) {
		String msg = "";
		int num = areaDao.areaEnable(params);
		if (num == 0) {
			msg = "区域启用失败！";
		}
		return msg;
	}

	@Override
	public String areaDisable(HashMap<String, String> params) {
		String msg = null;
		int num = areaDao.areaDisable(params);
		if (num == 0) {
			msg = "区域停用失败！";
		}
		return msg;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map areaByAreacode(HashMap<String, String> params) {
		return areaDao.areaByAreacode(params);
	}

	@Override
	public String areaTreebig(HashMap<String, String> params) {
		return areaDao.areaTreebig(params);
	}

}
